<html>
<head>
    <meta content="text/html; charset=utf-8" http-equiv="content-type">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
    <title>
        js调用java
    </title>
</head>

<body>
<p>
    <xmp id="show">
    </xmp>
</p>
<p>
    <xmp id="init">
    </xmp>
</p>
<p>
    <input type="text" id="text1" value="用户名(username)"/>
</p>
<p>
    <input type="text" id="text2" value="password"/>
</p>
<p>
    <input type="button" id="enter" value="发消息给Native" onclick="testDiv();"/>
</p>
<p>
    <input type="button" id="enter1" value="调用Native方法" onclick="testClick1();"/>
</p>
<p>
    <input type="button" id="enter3" value="调用Native方法2" onclick="testClick2();"/>
</p>
<p>
    <input type="button" id="enter2" value="显示html" onclick="testDiv();"/>
</p>
<p>
    <input type="button" id="enter4" value="js调用原生" onclick="test();"/>
</p>

<p>
    <input type="button" id="save" value="js调用原生save" onclick="save();"/>
</p>
<p>
    <input type="file" value="打开文件"/>
</p>
</body>
<script src="vconsole.min.js"></script>

<script>
  var vConsole = new VConsole();

</script>
<script>
        function testDiv() {
           window.WebViewJavascriptBridge.callHandler(
                'getData','aa'
                , function(responseData) {
                 var s = responseData.replace('[','').replace(']','')
                 console.log(s.split(','));
                    document.getElementById("show").innerHTML = "string, data = " + s.split(',');
                }
            );
        }

        function save() {
            window.WebViewJavascriptBridge.callHandler(
                'saveHistory'
                , 'save111e'
                , function(responseData) {
                    document.getElementById("show").innerHTML = "send get responseData from getdata, data = " + responseData
                }
            );
        }

        function testClick() {
            //send message to native
            var data = {id: 1, content: "这是一个图片 <img src=\"a.png\"/> test\r\nhahaha"};
            window.WebViewJavascriptBridge.send(
                data
                , function(responseData) {
                    document.getElementById("show").innerHTML = "response Data from java, data = " + responseData
                }
            );

        }

        function testClick1() {
            var str1 = document.getElementById("text1").value;
            var str2 = document.getElementById("text2").value;


            //call native method
            window.WebViewJavascriptBridge.callHandler(
                'submitFromWeb'
                , {'param': '中文测试'}
                , function(responseData) {
                    document.getElementById("show").innerHTML = "send get responseData from javas, data = " + responseData
                }
            );

        }

        function testClick2() {
            var str1 = document.getElementById("text1").value;
            var str2 = document.getElementById("text2").value;

            //call native method
            window.WebViewJavascriptBridge.callHandler(
                'submitFromWeb2'
                , {'param': '中文测试'}
                , function(responseData) {
                    document.getElementById("show").innerHTML = "send get responseData from java, data = " + responseData
                }
            );
        }

        function bridgeLog(logContent) {
            document.getElementById("show").innerHTML = logContent;
        }

        function connectWebViewJavascriptBridge(callback) {
            if (window.WebViewJavascriptBridge) {
                callback(WebViewJavascriptBridge)
            } else {
                document.addEventListener(
                    'WebViewJavascriptBridgeReady'
                    , function() {
                        callback(WebViewJavascriptBridge)
                    },
                    false
                );
            }
        }

        connectWebViewJavascriptBridge(function(bridge) {
            bridge.init(function(message, responseCallback) {
                console.log('JS got a message', message);
                var data = {
                    'Javascript Responds': '测试中文!'
                };

                if (responseCallback) {
                    console.log('JS responding with', data);
                    responseCallback(data);
                }
            });

            bridge.registerHandler("functionInJs", function(data, responseCallback) {
                document.getElementById("show").innerHTML = ("data from Java: = " + data);
                if (responseCallback) {
                    var responseData = "Javascript Says Right back aka!";
                    responseCallback(responseData);
                }
            });

            bridge.registerHandler("function", function(data, responseCallback) {
                document.getElementById("show").innerHTML = ("data from Java jsss: = " + data);
                if (responseCallback) {
                    var responseData = "Javascript Says Right back aka!";
                    responseCallback(responseData);
                }
            });

            bridge.registerHandler("getData", function(data, responseCallback) {
                document.getElementById("show").innerHTML = ("String list: = " + "\""+ data.toString()+"\"");
                console.log(data)
            });
        })

        function test(){
<!--            android.Test();-->

            window.WebViewJavascriptBridge.callHandler(
                'getData','aa'
                , function(responseData) {
                consoleo.log(responseData)
                    document.getElementById("show").innerHTML = "send get responseData from java, data = " + responseData
                }
            );
        }









































</script>

</html>

